#! /bin/sh

#  Copyright (c) International Business Machines  Corp., 2002
#
#  This program is free software;  you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY;  without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
#  the GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program;  if not, write to the Free Software
#  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

# 12/05/02  Port to bash -Robbie Williamson <robbiew@us.ibm.com>
# 02/05/03  Modified - Manoj Iyer <manjo@mail.utexas.edu> use USCTEST macros
#           fixed bugs.
# 07/26/05  Michael Reed  <mreedltp@vnet.ibm.com>
#           Made changes to account for the replacement of syslogd
#           with syslog-ng
#
##################################################################
# case 7: Test the priorities....                                #
#                                                                #
#         o Add lowest prority level i.e debug level entry to    #
#           configuration file.                                  #
#           o For syslog-ng the priority is set to all           #
#             because of the format of syslog-ng.conf            #
#             The format of the tests is the same, all levels of #
#             debug and above are logged                         #
#         o Send syslog messages at all levels and see whether   #
#           higher level messages are logged.                    #
##################################################################

. syslog-lib.sh || exit 1

syslog_case7()
{
	tst_resm TINFO "testing syslog priorities ..."

	# Adds some clarification of log message when syslog-ng is used
	if [ $CONFIG_FILE = /etc/syslog.conf ]; then
		explanation="Higher"
	else
		explanation="All"
	fi

	tst_resm TINFO " o Send syslog messages at all levels and see whether"
	tst_resm TINFO "   $explanation level messages are logged."

	# Create the configuration file specific to this test case.
	case "$CONFIG_FILE" in
	/etc/syslog.conf|/etc/rsyslog.conf)
	        echo "$RSYSLOG_CONFIG" > $CONFIG_FILE
	        echo "user.debug /var/log/messages" >> $CONFIG_FILE
		;;

	/etc/syslog-ng/syslog-ng.conf)
		echo "source src{ internal(); unix-dgram(\"/dev/log\"); udp(ip(\"0.0.0.0\") port(514)); };" > $CONFIG_FILE
		echo " " >> $CONFIG_FILE
		echo " " >> $CONFIG_FILE
		echo "# Added for syslog testcase" >> $CONFIG_FILE
		echo "filter f_syslog_messages {facility(user); };" >> $CONFIG_FILE
		echo "destination syslog-messages { file(\"/var/log/messages\");};" >> $CONFIG_FILE
		echo "log { source(src); filter(f_syslog_messages); destination(syslog-messages); };" >> $CONFIG_FILE
		;;
	esac

	restart_syslog_daemon

	if [ -e /var/log/messages ]; then
		emerg_old=`grep -c "syslogtst: emergency log" /var/log/messages`
		alert_old=`grep -c "syslogtst: alert log" /var/log/messages`
		crit_old=`grep -c "syslogtst: critical log" /var/log/messages`
		err_old=`grep -c "syslogtst: error log" /var/log/messages`
		warning_old=`grep -c "syslogtst: warning log" /var/log/messages`
		notice_old=`grep -c "syslogtst: notice log" /var/log/messages`
		info_old=`grep -c "syslogtst: info log" /var/log/messages`
		debug_old=`grep -c "syslogtst: debug log" /var/log/messages`
	else
		emerg_old=0
		alert_old=0
		crit_old=0
		err_old=0
		notice_old=0
		warning_old=0
		notice_old=0
		info_old=0
		debug_old=0
	fi

	# Call syslogtst. It will send the messages of all levels.
	if ! syslogtst 7 2>/dev/null; then
		cleanup 1
	fi
	sleep 2

	emerg_new=`grep -c "syslogtst: emergency log" /var/log/messages`
	alert_new=`grep -c "syslogtst: alert log" /var/log/messages`
	crit_new=`grep -c "syslogtst: critical log" /var/log/messages`
	err_new=`grep -c "syslogtst: error log" /var/log/messages`
	warning_new=`grep -c "syslogtst: warning log" /var/log/messages`
	notice_new=`grep -c "syslogtst: notice log" /var/log/messages`
	info_new=`grep -c "syslogtst: info log" /var/log/messages`
	debug_new=`grep -c "syslogtst: debug log" /var/log/messages`

	emerg=$(( $emerg_new - $emerg_old ))
	alert=$(( $alert_new - $alert_old ))
	crit=$(( $crit_new - $crit_old ))
	err=$(( $err_new - $err_old ))
	warning=$(( $warning_new - $warning_old ))
	notice=$(( $notice_new - $notice_old ))
	info=$(( $info_new - $info_old ))

	if [ $emerg -ne 1 -o $alert -ne 1 -o $crit -ne 1 -o $err -ne 1 -o \
	     $warning -ne 1 -o $notice -ne 1 -o $info -ne 1 -o \
	     $info -ne 1 ]; then
		status_flag=1
	fi
}

setup
syslog_case7
cleanup ${status_flag:=0}
